import Toolkit from '../Toolkit'
import AbstractMarker from './AbstractMarker'
import { circle, marker, polygon, polyline, rectangle, icon } from 'leaflet'
import markerIcon from 'leaflet/dist/images/marker-icon.png'
import markerIcon2x from 'leaflet/dist/images/marker-icon-2x.png'
import markerShadow from 'leaflet/dist/images/marker-shadow.png'
/**
 * @classdesc 标记，包含地图上的点线面
 *@author Niyang
 */
export default class Marker extends AbstractMarker {
    /**
     * @description 创建标记点
     * @param {String} symbol
     * @param {Object} cfg
     * @returns Marker
     */
    build (symbol, cfg) {
        let stamp = null
        switch (symbol) {
        case 'point':
            stamp = this.marker(cfg.latlng, cfg.options)
            break
        case 'polyline':
            stamp = polyline(cfg.latlng, cfg.options)
            break
        case 'polygon':
            stamp = polygon(cfg.latlng, cfg.options)
            break
        case 'rectangle':
            stamp = rectangle(cfg.bounds, cfg.options)
            break
        case 'circle':
            stamp = circle(cfg.latlng, cfg.options)
            break
        default:
            break
        }
        return stamp
    }

    /**
     * @description 获取marker
     * @param {Object} options
     * @returns Marker
     */
    getMarker (options) {
        const { symbol } = options
        if (Toolkit.isUndef(symbol)) {
            console.error(new Error('Marker require a symbol'))
            return null
        }
        return this.build(symbol, options)
    }

    /**
         * @description 解决默认图标不显示问题
         * @param {*} latlng
         * @param {*} options
         * @returns Marker
         */
    marker (latlng, options) {
        const defIcon = icon({
            iconUrl: markerIcon,
            iconRetinaUrl: markerIcon2x,
            shadowUrl: markerShadow,
            iconSize: [25, 41],
            iconAnchor: [12, 41],
            popupAnchor: [1, -34],
            tooltipAnchor: [16, -28],
            shadowSize: [41, 41]
        })
        return marker(latlng, Toolkit.assign({ icon: defIcon }, options))
    }
}
